﻿package com.leqicheng.tenon.util;

import java.util.ArrayList;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

/**
 * 銮峰彇缃戠粶钟舵€侊紝鎻愪緵缃戠粶钟舵€佸彉鍖栫洃鍚?CAUTION: 濡傛灉娉ㄥ唽浜嗙綉缁灭姸镐佺洃鍚紝锷″繀鍦ㄤ箣鍚庝笉鍐嶉渶瑕佺洃鍚殑镞跺€栾В闄ょ洃鍚€? * 濡傛灉鏄疉ctivity/Fragment绛夊璞″疄鐜颁简OnNetworkChangeListener骞舵敞鍐岀洃鍚紝
 * 锷″繀鍦╫nDestroy镄勬椂链栾В闄ょ洃鍚紝鍚﹀垯鍗充娇Activity琚玠estroy鎺夛紝鐢变簬链夊璇ctivity镄勫紩鐢紝
 * 灏嗕细瀵艰嚧璇ctivity镄勫唴瀛樻棤娉曢喷鏀俱€? * 
 * @author zhaoxinyang
 */
public final class NetworkUtil {

	// 缃戠粶鍙桦寲鐩戝惉钥卨ist
	private static final List<OnNetworkChangeListener> sNetworkChangeListeners = new ArrayList<OnNetworkChangeListener>();
	// 鐩戝惉缃戠粶鍙桦寲receiver
	private static final BroadcastReceiver sBroadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent
					.getAction())) {// 鐩戝惉缃戠粶杩炴帴钟舵€佸彉鍖?				
			    notifyListeners();
			}
		}
	};

	private NetworkUtil() {
	}

	/**
	 * 鍒濆鍖?	 */
	public static void init() {
		try {
			IntentFilter filter = new IntentFilter();
			filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
			ContextProvider.getApplicationContext().registerReceiver(
					sBroadcastReceiver, filter);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void notifyListeners() {
		for (OnNetworkChangeListener listener : sNetworkChangeListeners) {
			if (listener != null) {
				listener.onNetworkChanged();
			}
		}
	}

	/**
	 * 娉ㄥ唽缃戠粶钟舵€佸彉鍖栫洃鍚€?	 */
	public synchronized static void registerNetworkChangeListener(
			OnNetworkChangeListener listener) {
		if (listener != null && !sNetworkChangeListeners.contains(listener)) {
			sNetworkChangeListeners.add(listener);
		}
	}

	/**
	 * 瑙ｉ櫎缃戠粶钟舵€佺洃鍚?	 */
	public synchronized static void unregisterNetworkChangeListener(
			OnNetworkChangeListener listener) {
		if (listener != null && sNetworkChangeListeners.contains(listener)) {
			sNetworkChangeListeners.remove(listener);
		}
	}

	/**
	 * 缃戠粶钟舵€佸彉鍖栫洃鍚帴鍙?	 */
	public interface OnNetworkChangeListener {
		public void onNetworkChanged();
	}

	/**
	 * 缃戠粶杩炴帴鏄惁鍙敤
	 */
	public static boolean isNetAvailable() {
		return getNetworkType() != NETWORK_TYPE_NONE;
	}

	/**
	 * 娌℃湁鎺ュ叆绉诲姩铚傜猃缃?	 */
	public static final int NETWORK_OPERATOR_NONE = 0;
	/**
	 * 涓浗绉诲姩
	 */
	public static final int NETWORK_OPERATOR_CMCC = 1;
	/**
	 * 涓浗鑱旈€?	 */
	public static final int NETWORK_OPERATOR_CUCC = 2;
	/**
	 * 涓浗鐢典俊
	 */
	public static final int NETWORK_OPERATOR_CTCC = 3;
	/**
	 * 链煡杩愯惀鍟?	 */
	public static final int NETWORK_OPERATOR_UNKOWN = 4;

	/**
	 * 銮峰彇杩愯惀鍟?	 * 
	 * @return 鍙傝€僋ETWORK_OPERATOR_NONE;NETWORK_OPERATOR_CMCC;
	 *         NETWORK_OPERATOR_CUCC;
	 *         NETWORK_OPERATOR_CTCC;NETWORK_OPERATOR_UNKOWN
	 */
	// TODO:鍙屽崱镓嬫満鐢ㄦ锷炴硶鍒ゆ柇缃戠粶浼氭湁闂锛岃嚧鏂版彁渚涚殑鏂规硶杩斿洖涔熶笉鍑嗙‘锛屾湁寰呰繘涓€姝ユ洿鏂?	// public static int getMobileSubscriber() {
	// TelephonyManager telephonyManager = (TelephonyManager) ContextProvider
	// .getApplicationContext().getSystemService(
	// Context.TELEPHONY_SERVICE);
	// String operator = telephonyManager.getSimOperator();
	// if (operator == null || operator.equals("")) {
	// operator = telephonyManager.getSubscriberId();
	// }
	// if (operator != null) {
	// if (operator.startsWith("46000") || operator.startsWith("46002")) {
	// return NETWORK_OPERATOR_CMCC;
	// } else if (operator.startsWith("46001")
	// || operator.startsWith("46010")) {
	// return NETWORK_OPERATOR_CUCC;
	// } else if (operator.startsWith("46003")) {
	// return NETWORK_OPERATOR_CTCC;
	// } else {
	// return NETWORK_OPERATOR_UNKOWN;
	// }
	// } else {
	// return NETWORK_OPERATOR_NONE;
	// }
	// }

	/**
	 * 娌℃湁缃戠粶杩炴帴
	 */
	public static final int NETWORK_TYPE_NONE = 0;
	/**
	 * wifi
	 */
	public static final int NETWORK_TYPE_WIFI = 1;
	/**
	 * 2G缃戠粶杩炴帴
	 */
	public static final int NETWORK_TYPE_2G = 2;
	/**
	 * 3G缃戠粶杩炴帴
	 */
	public static final int NETWORK_TYPE_3G = 3;
	/**
	 * 4G缃戠粶杩炴帴
	 */
	public static final int NETWORK_TYPE_4G = 4;
	/**
	 * 链兘鍒ゆ柇缃戠粶绫诲瀷锛屽仛绉诲姩缃戠粶澶勭悊
	 */
	public static final int NETWORK_TYPE_MOBILE = 5;

	/**
	 * 銮峰彇缃戠粶绫诲瀷
	 * 
	 * @return 杩斿洖链硷紝鍙傝€僋ETWORK_TYPE_NONE;NETWORK_TYPE_WIFI;NETWORK_TYPE_2G;
	 *         NETWORK_TYPE_3G; NETWORK_TYPE_4G;NETWORK_TYPE_MOBILE
	 */
	public static int getNetworkType() {
		ConnectivityManager connectivityManager = (ConnectivityManager) ContextProvider
				.getApplicationContext().getSystemService(
						Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

		if (networkInfo != null && networkInfo.isAvailable()) {
			if (ConnectivityManager.TYPE_WIFI == networkInfo.getType()) {
				return NETWORK_TYPE_WIFI;
			} else {
				TelephonyManager telephonyManager = (TelephonyManager) ContextProvider
						.getApplicationContext().getSystemService(
								Context.TELEPHONY_SERVICE);
				int networkType = telephonyManager.getNetworkType();
				switch (networkType) {
				case TelephonyManager.NETWORK_TYPE_GPRS:
				case TelephonyManager.NETWORK_TYPE_EDGE:
				case TelephonyManager.NETWORK_TYPE_CDMA:
				case TelephonyManager.NETWORK_TYPE_1xRTT:
				case TelephonyManager.NETWORK_TYPE_IDEN:
					return NETWORK_TYPE_2G;
				case TelephonyManager.NETWORK_TYPE_UMTS:
				case TelephonyManager.NETWORK_TYPE_EVDO_0:
				case TelephonyManager.NETWORK_TYPE_EVDO_A:
				case TelephonyManager.NETWORK_TYPE_HSDPA:
				case TelephonyManager.NETWORK_TYPE_HSUPA:
				case TelephonyManager.NETWORK_TYPE_HSPA:
				case TelephonyManager.NETWORK_TYPE_EVDO_B:
				case TelephonyManager.NETWORK_TYPE_EHRPD:
				case TelephonyManager.NETWORK_TYPE_HSPAP:
					return NETWORK_TYPE_3G;
				case TelephonyManager.NETWORK_TYPE_LTE:
					return NETWORK_TYPE_4G;
				default:
					return NETWORK_TYPE_MOBILE;
				}
			}
		} else {
			return NETWORK_TYPE_NONE;
		}
	}

	/**
	 * 褰揿墠缃戠粶鏄惁鏄痹ifi
	 * 
	 * @return
	 */
	public static boolean isWIFI() {
		int netType = getNetworkType();
		if (netType == NetworkUtil.NETWORK_TYPE_WIFI) {
			return true;
		}
		return false;
	}

	/**
	 * 褰揿墠缃戠粶鏄惁鏄?銆?銆?G
	 * 
	 * @return
	 */
	public static boolean isMobileNet() {
		int netType = getNetworkType();
		switch (netType) {
		case NetworkUtil.NETWORK_TYPE_2G:
		case NetworkUtil.NETWORK_TYPE_3G:
		case NetworkUtil.NETWORK_TYPE_4G:
		case NetworkUtil.NETWORK_TYPE_MOBILE:
			return true;
		default:
			break;
		}
		return false;
	}
}
